import React, { Component } from "react"
import nenus from './../../route/menus'
import './index.css'
import { Layout, Menu } from 'antd';
import{ 
  // MailOutlined
} from '@ant-design/icons'
// 路由
import { withRouter } from 'react-router';
const {  Sider } = Layout;

const { SubMenu } = Menu;
class Com extends Component {
  state = {
    collapsed: false,
  };
  renderlist = (nenus) =>{
    // 设置权限
    const roleType = localStorage.getItem('roleType') * 1
    console.log(roleType) 
    return nenus.map(item => {
      // console.log(item)
      if (item.children) { //如果有二级
        if (item.permission > roleType) {
          return null
        }
        return (
        <SubMenu
            key={item.path}
            title={
              <span>
                <span>{item.title}</span>
              </span>
            }
          >
            {/* 递归回调 */}
            {this.renderlist(item.children)}
            {/* <Menu.Item key="1">Option 1</Menu.Item> */}
          </SubMenu>
        )
      }else{
        if (item.permission > roleType) {
          return null
        }
        return (
          <Menu.Item key={item.path}>
            <item.icon />
              <span>{item.title}</span>
           </Menu.Item>
        ) 
      }
    })
  }
  carlist = (obj) => {
    console.log(obj);
    console.log(this.props) //孔数据引入路由
    this.props.history.push(obj.key)
  }
  render(){
    console.log(this.props) // 默认打开开始
    const pathname = this.props.location.pathname
    const OpenKeys = ['/' + pathname.split('/')[1]]
    const SelectedKeys = [pathname]
    return (
      <Sider trigger={null} collapsible collapsed={this.state.collapsed}>
        <div className="logo" />
        <Menu theme="dark" mode="inline" onClick = { this.carlist } defaultOpenKeys={OpenKeys} 
        defaultSelectedKeys={SelectedKeys}>
          {/* <Menu.Item key="1">
            <UserOutlined />
            <span>首页</span>
          </Menu.Item> */}
          {this.renderlist(nenus)}
        </Menu>
      </Sider>
     
    )
  }
}
export default withRouter(Com)